var ajax = require('../../utils/ajax.js');
var app = getApp();

Page({
    data: {
        dataLoaded: true,
        luckBagList: [],
        shopInfo: {},
        page: 1,
        loading: false,
        moreLoading: false,
        noMore: false,
        title: ''
    },

    onLoad(options) {
        var _self = this;
        this._options = options;
        /**
         * 获取店铺信息
         */
        var url = '/openapi/User/getQueuePeople';
        var paramsData = {
            shopId: this._options.shopId
        };
        ajax.P(url, paramsData, function(data) {
            _self.setData({
                shopInfo: data.result.shopInfo
            })
        })

        this._toTab();
    },
    onReachBottom: function() {
        this._loadMore();
    },
    _back() {
        wx.navigateBack();
    },

    _toTab: function(cb) {
        this.setData({
            luckBagList: [],
            page: 1,
            noMore: false,
        });
        this._getLuckBagRecord({}, cb);
    },

    _getLuckBagRecord: function(params, cb) {
        var self = this;
        var isMore = params.isMore;
        var page = self.data.page || 1;
        var url = "/openapi/User/shopHongbaoLog";

        if (isMore) {
            self.setData({
                moreLoading: true
            });
        } else {
            self.setData({
                loading: true
            });
        }
        ajax.G(url, {
            page: page,
            shopid: this._options.shopId
        }, function(data) {
            var list = data.result.list || [];
            var luckBagList = self.data.luckBagList;
            var toData = {
                luckBagList: luckBagList.concat(list),
                noMore: list.length == 0,
                page: self.data.page + 1,
                title: data.result.title
            };
            toData[isMore ? 'moreLoading' : 'loading'] = false;
            self.setData(toData);
            cb && cb();
        });
    },

    /**
     * 加载更多
     */
    _loadMore: function() {
        console.log('---_loadMore')
        if (this.data.moreLoading || this.data.loading || this.data.noMore) {
            return;
        }
        this._getLuckBagRecord({
            isMore: true
        });
    },
});